/**
 * 
 */
package net.toocruel.iqismart.service.core.impl;


import net.toocruel.iqismart.dao.UserRepository;
import net.toocruel.iqismart.dao.core.AnswerRepository;
import net.toocruel.iqismart.dao.core.ArticleRepository;
import net.toocruel.iqismart.dao.core.CommentRepository;
import net.toocruel.iqismart.dao.core.QuestionRepository;
import net.toocruel.iqismart.entity.constants.NotificationType;
import net.toocruel.iqismart.entity.constants.SourceType;
import net.toocruel.iqismart.entity.core.*;
import net.toocruel.iqismart.entity.rbac.User;
import net.toocruel.iqismart.service.UserService;
import net.toocruel.iqismart.service.core.CommentService;
import net.toocruel.iqismart.service.core.NotificationService;
import net.toocruel.iqismart.service.helper.UserHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

/**
 * @author toocruel
 *
 */
@Service
@Transactional
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentRepository commentRepository;

    @Autowired
    private QuestionRepository questionRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private AnswerRepository answerRepository;

    @Autowired
    private ArticleRepository articleRepository;

    @Autowired
    private UserService userService;
    @Autowired
    private NotificationService notificationService;
    /**
     * @param comment 新建评论
     * @return
     */
    @Override
    public Comment postComment(Comment comment) {
        //设置状态 默认审核通过 后期根据配置决定是否进入审核
        comment.setStatus(1);
        comment.setUser(UserHelper.getCurrentUser());
        User sourceUser = null;
        //关联评论对象
        switch (comment.getSourceType()){
            case question:
                Question question = questionRepository.findOne(comment.getQuestionId());
                comment.setQuestion(question);
                comment.setToUser(question.getUser());
                sourceUser = question.getUser();
                break;
            case article:
                Article article = articleRepository.findOne(comment.getArticleId());
                article.setCommentNum(article.getCommentNum()+1);
                article.setLastAnswer(System.currentTimeMillis());
                comment.setArticle(article);
                comment.setToUser(article.getUser());
                sourceUser = article.getUser();
                break;
            case answer:
                Answer answer = answerRepository.findOne(comment.getAnswerId());
                comment.setAnswer(answer);
                comment.setToUser(answer.getUser());
                sourceUser = answer.getUser();
                break;
            default:
        }

        if(comment.getToUserId()!=null && comment.getToUserId()>0){
            comment.setToUser(userRepository.findById(comment.getToUserId()));
        }

        if(comment.getToCommentId()!=null && comment.getToCommentId()>0){
            comment.setToComment(commentRepository.findOne(comment.getToCommentId()));
        }




        comment = commentRepository.saveAndFlush(comment);



        //TODO 积分奖励

        Set<NotificationType> siteNotifications = sourceUser.getSiteNotifications();
        if(siteNotifications.contains(NotificationType.COMMENT_ANSWER)||
                siteNotifications.contains(NotificationType.COMMENT_QUESTION)||
                        siteNotifications.contains(NotificationType.COMMENT_ARTICLE)) {
            if(siteNotifications.contains(NotificationType.COMMENT_ARTICLE)){
                Notification notification = new Notification();
                notification.setType(NotificationType.COMMENT_ARTICLE);
                notification.setToUser(comment.getToUser());
                notification.setFromUser(comment.getUser());
                notification.setArticle(comment.getArticle());
                notification.setComment(comment);
                notificationService.send(notification);
            }
            if(comment.getToUser().getEmailNotifications().contains(NotificationType.COMMENT_ARTICLE)){
                //TODO 发送邮件
            }

            if(siteNotifications.contains(NotificationType.COMMENT_QUESTION)){
                Notification notification = new Notification();
                notification.setType(NotificationType.COMMENT_QUESTION);
                notification.setToUser(comment.getToUser());
                notification.setFromUser(comment.getUser());
                notification.setQuestion(comment.getQuestion());
                notification.setComment(comment);
                notificationService.send(notification);
            }
            if(comment.getToUser().getEmailNotifications().contains(NotificationType.COMMENT_QUESTION)){
                //TODO 发送邮件
            }

            if(siteNotifications.contains(NotificationType.COMMENT_ANSWER)){
                Notification notification = new Notification();
                notification.setType(NotificationType.COMMENT_ANSWER);
                notification.setToUser(comment.getToUser());
                notification.setFromUser(comment.getUser());
                notification.setQuestion(comment.getQuestion());
                notification.setAnswer(comment.getAnswer());
                notification.setComment(comment);
                notificationService.send(notification);
            }
            if(comment.getToUser().getEmailNotifications().contains(NotificationType.COMMENT_ANSWER)){
                //TODO 发送邮件
            }
        }

        if(comment.getToUser()!=null){
            if(comment.getToUser().getSiteNotifications().contains(NotificationType.REPLY_COMMENT)){
                Notification notification = new Notification();
                notification.setType(NotificationType.REPLY_COMMENT);
                notification.setToUser(comment.getToUser());
                notification.setFromUser(comment.getUser());
                notification.setQuestion(comment.getQuestion());
                notification.setAnswer(comment.getAnswer());
                notification.setComment(comment);
                notificationService.send(notification);
            }
            if(comment.getToUser().getEmailNotifications().contains(NotificationType.REPLY_COMMENT)){
                //TODO 发送邮件
            }
        }




        return comment;
    }

    /**
     * @param sourceType 评论类型
     * @param sourceId   评论对象ID 问题 文章等
     * @return
     */
    @Override
    public Page<Comment> getCommonList(SourceType sourceType, Long sourceId, Pageable pageable) {
        String sourceColume = "";
        sourceColume = getSourceColumeName(sourceType, sourceColume);
        String finalSourceColume = sourceColume;
        return commentRepository.findAll((root, query, cb) -> cb.and(cb.equal(root.get("sourceType"),sourceType),cb.equal(root.get(finalSourceColume),sourceId)),pageable);
    }

    /**
     * 根据评论类型获取对象名称
     * @param sourceType
     * @param sourceColume
     * @return
     */
    private String getSourceColumeName(SourceType sourceType, String sourceColume) {
        switch (sourceType){
            case question:
                sourceColume = "question";
                break;
            case article:
                sourceColume = "article";
                break;
            case answer:
                sourceColume = "answer";
                break;
            case comment:
                break;
            default:
        }
        return sourceColume;
    }
}
